#include "BspSpi.h"


/******************************************************************************
* @brief  BspSPI1_IO_Conifg
* @param  None
* @retval None
* @IO PA5---------------SPI1_SCK
* @IO PA6---------------SPI1_SDO
* @IO PA7---------------SPI1_SDI
*******************************************************************************/
static void BspSPI1_IO_Conifg(void)
{
  GPIO_InitTypeDef GPIOX;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,ENABLE);
	
	GPIOX.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
	GPIOX.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIOX.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIOX);
}

/******************************************************************************
* @brief  BspSPI1_Config
* @param  None
* @retval None
* @IO     None
*******************************************************************************/
void BspSPI1_Config(void)
{
  SPI_InitTypeDef   SPIX;
	
  BspSPI1_IO_Conifg();
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
	
	SPIX.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
	SPIX.SPI_CPHA = SPI_CPHA_2Edge;
	SPIX.SPI_CPOL = SPI_CPOL_High;
	SPIX.SPI_CRCPolynomial = 7;
	SPIX.SPI_DataSize = SPI_DataSize_8b;
	SPIX.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPIX.SPI_FirstBit = SPI_FirstBit_MSB;
	SPIX.SPI_Mode = SPI_Mode_Master;
	SPIX.SPI_NSS = SPI_NSS_Soft;
	SPI_Init(SPI1,&SPIX);
	SPI_Cmd(SPI1,ENABLE);
}

/*********************************************************************************************
  * @brief  Bsp_SPI1_SendByte
  * @param  uint8_t Byte 需要发送的字节数据
  * @retval uint8_t 读取的字节数据
  ********************************************************************************************/
uint8_t Bsp_SPI1_SendByte(uint8_t Byte)
{
  /*!< Loop while DR register in not empty */
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

  /*!< Send Half Word through the sFLASH peripheral */
  SPI_I2S_SendData(SPI1, Byte);

  /*!< Wait to receive a Half Word */
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

  /*!< Return the Half Word read from the SPI bus */
  return SPI_I2S_ReceiveData(SPI1);
}




